package service

import (
	"context"
	"encoding/json"
	"fmt"
	"math/rand"
	"strconv"
	"time"
	"yueyue/yue-rpc/handler/config"
	__ "yueyue/yue-rpc/handler/proto"
	"yueyue/yue-rpc/pkg"
	"yueyue/yue-rpc/service/models"
)

type Server struct {
	__.UnimplementedYueYueServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) SendSms(_ context.Context, in *__.SendSmsReq) (*__.SendSmsResp, error) {
	code := rand.Intn(900000) + 100000
	//config.Rdb.Get(config.Ctx, "sendSms"+in.Mobile)
	get := config.Rdb.Get(config.Ctx, "sendSms"+in.Mobile)
	if get.Val() > "3" {
		return nil, fmt.Errorf("发送次数过多")
	}
	//sms, err := pkg.SendSms(in.Mobile, strconv.Itoa(code))
	//if err != nil {
	//	return nil, err
	//}
	//if *sms.Body.Message != "OK" {
	//	return nil, fmt.Errorf(*sms.Body.Code)
	//}
	config.Rdb.Incr(config.Ctx, "sendSms"+in.Mobile)
	config.Rdb.Expire(config.Ctx, "sendSms"+in.Mobile, time.Minute*5)
	config.Rdb.Set(config.Ctx, "sendSms"+in.Mobile, code, time.Minute*10)
	mq := pkg.NewRabbitMQSimple("sendSms")
	mq.PublishSimple(strconv.Itoa(code))
	return &__.SendSmsResp{}, nil
}
func (s *Server) Register(_ context.Context, in *__.RegisterReq) (*__.RegisterResp, error) {
	user := models.User{
		Mobile:   in.Mobile,
		Password: in.Password,
	}
	get := config.Rdb.Get(config.Ctx, "sendSms"+in.Mobile)
	if get.Val() != in.SendSms {
		return nil, fmt.Errorf("验证码错误")
	}
	if models.Register(&user) != nil {
		return nil, fmt.Errorf("注册失败")
	}
	return &__.RegisterResp{
		Id: int64(user.ID),
	}, nil
}
func (s *Server) Login(_ context.Context, in *__.LoginReq) (*__.LoginResp, error) {
	user := models.User{
		Mobile: in.Mobile,
	}
	get := config.Rdb.Get(config.Ctx, "sendSms"+in.Mobile)
	if get.Val() != in.SendSms {
		return nil, fmt.Errorf("验证码错误")
	}
	if models.Login(&user) != nil {
		return nil, fmt.Errorf("登录失败")
	}
	return &__.LoginResp{
		Id: int64(user.ID),
	}, nil
}
func (s *Server) VideoAdd(_ context.Context, in *__.VideoAddReq) (*__.VideoAddResp, error) {
	video := models.Video{
		Title:   in.Title,
		Message: in.Message,
		Static:  in.Static,
		Address: in.Address,
		Image:   in.Image,
	}
	if models.VideoAdd(&video) != nil {
		return nil, fmt.Errorf("视频录入失败")
	}
	return &__.VideoAddResp{
		Id: int64(video.ID),
	}, nil
}
func (s *Server) VideoList(_ context.Context, in *__.VideoListReq) (*__.VideoListResp, error) {
	var video []models.Video
	get := config.Rdb.Get(config.Ctx, "videoList")
	if get.Val() == "" {
		config.DB.Find(&video)
		marshal, _ := json.Marshal(video)
		config.Rdb.Set(config.Ctx, "videoList", marshal, time.Minute*10)
	} else {
		json.Unmarshal([]byte(get.Val()), &video)
	}
	if models.VideoList(&video, int(in.Page), int(in.Size), in.Address) != nil {
		return nil, fmt.Errorf("查询失败")
	}
	var lists []*__.List
	for _, m := range video {
		list := __.List{
			Title:   m.Title,
			Message: m.Message,
			Static:  m.Static,
			Address: m.Address,
			Image:   m.Image,
		}
		lists = append(lists, &list)
	}
	return &__.VideoListResp{
		Lists: lists,
	}, nil
}
func (s *Server) VideoDetails(_ context.Context, in *__.VideoDetailsReq) (*__.VideoDetailsResp, error) {
	var video models.Video
	if models.VideoDetails(&video, int(in.Id)) != nil {
		return nil, fmt.Errorf("查询失败")
	}
	return &__.VideoDetailsResp{
		Title:     video.Title,
		Message:   video.Message,
		Static:    video.Static,
		Address:   video.Address,
		Image:     video.Image,
		ViewCount: int64(video.ViewCount),
	}, nil
}
func (s *Server) Episode(_ context.Context, in *__.EpisodeListReq) (*__.EpisodeListResp, error) {
	var episode []models.Episode
	if models.EpisodeList(&episode) != nil {
		return nil, fmt.Errorf("查询失败")
	}
	var lists []*__.List1
	for _, m := range episode {
		list := __.List1{
			Title:      m.Title,
			Episode:    m.Episode,
			Time:       m.Time,
			PlayedTime: m.PlayedTime,
		}
		lists = append(lists, &list)
	}
	return &__.EpisodeListResp{
		Lists: lists,
	}, nil
}
func (s *Server) VideoType(_ context.Context, in *__.VideoTypeReq) (*__.VideoTypeResp, error) {
	var video models.Video
	if models.VideoType(&video, in.Static) != nil {
		return nil, fmt.Errorf("查询失败")
	}
	return &__.VideoTypeResp{
		Title:     video.Title,
		Message:   video.Message,
		Address:   video.Address,
		Image:     video.Image,
		ViewCount: int64(video.ViewCount),
	}, nil
}
func (s *Server) HistoryAdd(_ context.Context, in *__.HistoryAddReq) (*__.HistoryAddResp, error) {
	history := models.History{
		VideoId:    int(in.VideoId),
		EpisodeId:  int(in.EpisodeId),
		Image:      in.Image,
		PlayedTime: in.PlayedTime,
	}
	if models.HistoryAdd(&history) != nil {
		return nil, fmt.Errorf("添加失败")
	}
	return &__.HistoryAddResp{
		Id: int64(history.ID),
	}, nil
}
func (s *Server) HistoryList(_ context.Context, in *__.HistoryListReq) (*__.HistoryListResp, error) {
	var history models.History
	if models.HistoryList(&history) != nil {
		return nil, fmt.Errorf("查询失败")
	}
	return &__.HistoryListResp{
		Id:         int64(history.ID),
		VideoId:    int64(history.VideoId),
		EpisodeId:  int64(history.EpisodeId),
		Image:      history.Image,
		PlayedTime: history.PlayedTime,
	}, nil
}
func (s *Server) HotCalculate(_ context.Context, in *__.HotCalculateReq) (*__.HotCalculateResp, error) {
	var hot models.Hot
	if models.HotCalculate(&hot, int(in.VideoId)) != nil {
		return nil, fmt.Errorf("查询失败")
	}
	var hotnum float64
	hotnum = float64((in.ViewCount * 0.6) + (in.LikeCount * 0.2) + (in.CollCount * 0.1) + (in.CommentCount * 0.1))
	return &__.HotCalculateResp{
		Hot: float32(hotnum),
	}, nil
}
func (s *Server) HotList(_ context.Context, in *__.HotListReq) (*__.HotListResp, error) {
	var hot models.Hot
	if models.HotList(&hot) != nil {
		return nil, fmt.Errorf("查询失败")
	}
	return &__.HotListResp{
		Hot:          float32(hot.ID),
		VideoId:      int64(hot.VideoId),
		ViewCount:    float32(hot.ViewCount),
		LikeCount:    float32(hot.LikeCount),
		CollCount:    float32(hot.CollCount),
		CommentCount: float32(hot.CommentCount),
	}, nil
}
